package com.ganxiang.ximenhong.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ganxiang.ximenhong.R;
import com.ganxiang.ximenhong.base.BaseFragment;
import com.ganxiang.ximenhong.config.ImageConfig;
import com.ganxiang.ximenhong.util.DensityUtils;
import com.ganxiang.ximenhong.view.GoodsListGv;
import com.ganxiang.ximenhong.widght.AutoGallery;
import com.ganxiang.ximenhong.widght.AutoVerticalScrollTextView;
import com.ganxiang.ximenhong.widght.PageGuide;
import com.ganxiang.ximenhong.widght.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/13 0013.
 */
public class HomePageFragment extends BaseFragment{

    private View mView;
    private AutoVerticalScrollTextView mAutoVerticalScrollTextView;
    private boolean mIsRuning = true;
    private int mAutoVTvScollNum;
    String[]  s = {"恭喜广州市天河区1987483738获得电冰箱一台","恭喜广州市天河区1987483738获得电冰箱一台"};
    private List<String> mImageUrls = new ArrayList<>();
    private AutoGallery mAutoGallery;
    private AutoGalleryAdapter mAutoGalleryAdapter;
    private PageGuide mPageGuide;
    private RefreshLayout mRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = View.inflate(mActivity, R.layout.fragment_homepage,null);
        ((LinearLayout)mView.findViewById(R.id.ll_gv)).addView(new GoodsListGv(mActivity));
        initAutoVersiterTv();
        initAutoGalley();
        initRefreshLayout();
        return mView;
    }

    private void initAutoVersiterTv(){
        mAutoVerticalScrollTextView = (AutoVerticalScrollTextView) mView.findViewById(R.id.tv_VerticalScroll);
        new Thread(){
            @Override
            public void run() {
                while (mIsRuning){
                    SystemClock.sleep(3000);
                    mHandler.sendEmptyMessage(0);
                }
            }
        }.start();
    }

    private void initRefreshLayout(){
        mRefreshLayout = (RefreshLayout) mView.findViewById(R.id.refreshLayout);
        //mRefreshLayout.setRefreshing();

    }

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mAutoVerticalScrollTextView.next();
            mAutoVTvScollNum++;
            mAutoVerticalScrollTextView.setText(s[(mAutoVTvScollNum%s.length)]);
        }
    };


    private void initAutoGalley() {
        mImageUrls.add("http://gtms04.alicdn.com/tps/i4/TB1tzlmNXXXXXXJXFXXSutbFXXX.jpg_.webp");
        mImageUrls.add("http://i.mmcdn.cn/simba/img/TB16EuWMVXXXXbkXFXXSutbFXXX.jpg");
        mImageUrls.add("http://ossgw.alicdn.com/creatives-assets/61df12cc-ffae-423e-8535-49e8f44bd670.png");
        mImageUrls.add("http://img.alicdn.com/imgextra/i2/263817957/TB2mxhZXai5V1Bjy1zdXXaRkVXa-263817957.jpg_.webp");

        mAutoGallery = (AutoGallery) mView.findViewById(R.id.auotGallery);
        mAutoGalleryAdapter = new AutoGalleryAdapter();
        mAutoGallery.setAdapter(mAutoGalleryAdapter);
        mAutoGallery.setSelection(4 * 1000000);
        mAutoGallery.setLength(4);
        mAutoGallery.setDuration(2000);
        mAutoGallery.setAutoScroll();

        mPageGuide = (PageGuide) mView.findViewById(R.id.pg_indicator);
        mPageGuide.setRes(R.drawable.shape_qiehuanyuan_pre,R.drawable.shape_qiehuanyuan_pnor);
        mPageGuide.setParams(mImageUrls.size(), DensityUtils.dp2px(mActivity,7), DensityUtils.dp2px(mActivity,7));

        mAutoGallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mPageGuide.setSelect(i%mImageUrls.size());

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    class AutoGalleryAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mImageUrls == null ? 0 : Integer.MAX_VALUE;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("NewApi")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new ImageView(mActivity);
            }
            if (position >= mImageUrls.size()) {
                position = position % mImageUrls.size();
            }
            ((ImageView) convertView).setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.MATCH_PARENT, Gallery.LayoutParams.MATCH_PARENT));
            ((ImageView) convertView).setScaleType(ImageView.ScaleType.CENTER_CROP);
            ImageConfig.displayImage(mImageUrls.get(position), (ImageView) convertView);
            return convertView;
        }
    }
}
